package net.onecloud.onecloud.network;

import com.facebook.stetho.okhttp3.StethoInterceptor;

import net.onecloud.onecloud.network.service.AccountService;
import net.onecloud.onecloud.network.service.EmployService;
import net.onecloud.onecloud.network.service.EnterpriseService;
import net.onecloud.onecloud.network.service.PostService;
import net.onecloud.onecloud.network.service.RecordService;
import net.onecloud.onecloud.network.service.ResumeService;
import net.onecloud.onecloud.network.service.ToemployService;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zaizai on 16/7/11.
 */
public class NetWork {
    private static AccountService accountService;
    private static EmployService employService;
    private static OkHttpClient okHttpClient;
    private static Retrofit retrofit;
    private static HttpLoggingInterceptor httpLoggingInterceptor;

    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJavaCallAdapterFactory.create();

    static {
        httpLoggingInterceptor = new HttpLoggingInterceptor();

        okHttpClient = new OkHttpClient()
                .newBuilder()
                .addInterceptor(httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                .addNetworkInterceptor(new StethoInterceptor())
                .build();
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(NetConfig.HTTP_HOST)
                .addConverterFactory(gsonConverterFactory)
                .addCallAdapterFactory(rxJavaCallAdapterFactory)
                .build();
    }

    private static PostService postService;
    private static ResumeService resumeService;
    private static ToemployService toemployService;
    private static EnterpriseService enterpriseService;
    private static RecordService recordService;


    public static AccountService getAccountService() {
        if (accountService == null) {

            accountService = retrofit.create(AccountService.class);
        }
        return accountService;
    }

    public static EmployService getEmployApi() {
        if (employService == null) {
            employService = retrofit.create(EmployService.class);
        }
        return employService;
    }

    public static PostService getPostApi() {
        if (postService == null) {
            postService = retrofit.create(PostService.class);
        }
        return postService;

    }

    public static ResumeService getResumeApi() {
        if (resumeService == null) {
            resumeService = retrofit.create(ResumeService.class);
        }
        return resumeService;

    }

    public static ToemployService getToemployApi() {
        if (toemployService == null) {
            toemployService = retrofit.create(ToemployService.class);
        }
        return toemployService;
    }

    public static EnterpriseService getEnterpriseApi() {
        if (enterpriseService == null) {
            enterpriseService = retrofit.create(EnterpriseService.class);
        }
        return enterpriseService;
    }

    public static RecordService getRecordApi() {
        if (recordService == null) {
            recordService = retrofit.create(RecordService.class);
        }
        return recordService;
    }
}
